package com.everwin.web.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.everwin.common.config.Global;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger2的接口配置
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    private static final String SPLITOR = ";";

    Predicate<RequestHandler> selector01 = RequestHandlerSelectors.basePackage("com.everwin.web.controller.common");
    Predicate<RequestHandler> selector02 = RequestHandlerSelectors.basePackage("com.everwin.web.controller.home");
    Predicate<RequestHandler> selector03 = RequestHandlerSelectors.basePackage("com.everwin.web.controller.monitor");
    Predicate<RequestHandler> selector04 = RequestHandlerSelectors.basePackage("com.everwin.web.controller.system");



    /**
     * 创建API
     */
    @Bean
    public Docket createRestApi()
    {
        return new Docket(DocumentationType.SWAGGER_2)
                // 详细定制
                .apiInfo(apiInfo())
                .select()
                .apis(Predicates.or(selector01,selector02,selector03,selector04))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 添加摘要信息
     */
    private ApiInfo apiInfo()
    {
        // 用ApiInfoBuilder进行定制
        return new ApiInfoBuilder()
                    .title("穗禾接口文档")
                .description("对于项目提供的接口进行信息描述")
                .termsOfServiceUrl("http://everwin.cn")
                .contact(new Contact(Global.getName(), null, null))
                .version(Global.getVersion())
                .build();
    }

}
